/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vtc.app;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

/**
 *
 * @author Admin
 */
public abstract class GameCanvasBase extends GameCanvas implements Runnable {

    public int w = getWidth();
    public int h = getHeight();
    public int sleep = 30;
    public Player p;
    private boolean running = true;

    public GameCanvasBase(boolean suppressKeyEvents) {
        super(suppressKeyEvents);
    }

    public void startGame() {
        Thread t = new Thread(this);
        t.start();
    }

    public void stopGame() {
        this.running = false;
    }

    public abstract void runGame();

    public abstract void render(Graphics g);

    public abstract void checkKey(int key);

//    public abstract void checkKeyUpDown(int key);

    public TiledLayer drawMap(String mapFile, Image img) {
        TiledLayer map = null;
        DataInputStream ds = null;
        try {
            InputStream is = this.getClass().getResourceAsStream(mapFile);
            ds = new DataInputStream(is);
            ds.skipBytes(8);
            int mW = ds.readByte();
            int mH = ds.readByte();
            int tW = ds.readByte();
            int tH = ds.readByte();
            map = new TiledLayer(mW, mH, img, tW, tH);
            for (int i = 0; i < mH; i++) {
                for (int j = 0; j < mW; j++) {
                    int index = ds.readByte();
                    if (index > 0) {
                        map.setCell(j, i, index + 1);
                    }
                }
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                ds.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return map;
    }

    public void playSound(String path) {
        try {

            if (p == null) {
                InputStream in = this.getClass().getResourceAsStream(path);
                p = Manager.createPlayer(in, "audio/x-wav");
            }
            p.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        while (running) {
            this.runGame();
            try {
                Thread.sleep(sleep);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }

    }
}
